package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.*;
import com.burgeon.backend.SHOP.mappers.GroupMapper;
import com.burgeon.backend.SHOP.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    private GroupMapper groupMapper;

    @Override
    public String showImg(String token) {
        return groupMapper.showImg(token)+"?"+((Math.random() * 9 + 1) * 100000);
    }

    @Override
    public List<GroupItem> getGroupItem(String id) {
        return groupMapper.getGroupItem(id);
    }

    @Override
    public List<Group> showGroup(String token) {
        List<Group> groupList = groupMapper.showGroup(token);//查询拼团商品
        for (Group group : groupList) {
            Goods goods = groupMapper.showGoodsById(group.getGid());
           // group.setSum(groupMapper.showGroupSumByGrid(group.getId()));
            group.setGoods(goods);
            int count = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd" + " " + "HH:mm:ss");
            List<GroupOrder> groupOrders = groupMapper.getGroupOrderByGid(group.getGid());
            for (GroupOrder groupOrder1 : groupOrders){
                try {
                    if (sdf.parse(groupOrder1.getEndTime()).getTime()>new Date().getTime()&&"1".equals(groupOrder1.getState())){

                        count += groupOrder1.getNowNumber();

                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            group.setSum(String.valueOf(count));
        }


        return groupList;
    }

    @Override
    public HashMap getGoodsGroupList(String gid, String uid) {
        CopyOnWriteArrayList<GroupVO> groupVOS = groupMapper.showGoodsGroup(gid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd" + " " + "HH:mm:ss");

        int count = 0;
        List<GroupVO> data = new ArrayList<GroupVO>();
        for (GroupVO groupVO : groupVOS) {

            groupVO.setIsPay("N");
            if (!"0".equals(groupVO.getNum())) {
                if (!"null".equals(uid)) {
                    //获取item 判断是否存在uid
                    List<GroupItem> groupItems = groupMapper.getGroupItem(groupVO.getId());

                    for (GroupItem groupItem : groupItems) {
                        if (groupItem.getUid().equals(uid)) {
                            groupVO.setIsPay("Y");
                        }
                    }
                }
                //count += (Integer.parseInt(groupVO.getSumr()) - Integer.parseInt(groupVO.getNum()));

                try {
                    if (sdf.parse(groupVO.getEndTime()).getTime()>new Date().getTime()){
                        data.add(groupVO);
                        count += (Integer.parseInt(groupVO.getSumr()) - Integer.parseInt(groupVO.getNum()));
                        //groupVOS.remove(groupVO);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        HashMap hashMap = new HashMap();
        hashMap.put("count", count);
        hashMap.put("data", data);

        return hashMap;
    }
}
